/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.wizardpager.wizard.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.android.wizardpager.wizard.model.Page;
import com.example.android.wizardpager.wizard.model.PicturePage;
import com.example.goodog.R;
import com.example.goodog.customViews.UploadImageButton;
import com.example.goodog.interfaces.GetInfoCallback;

public class PictureFragment extends Fragment {
	private static final String ARG_KEY = "key";
	private static final int CAM_COMMAND = 1;
	private static final int GAL_COMMAND = 2;
	private static final int INT_COMMAND = 3;

    private PageFragmentCallbacks mCallbacks;
    private String mKey;
    private Page mPage;
    private String label;
    
    private UploadImageButton uib;

    public static PictureFragment create(String key) {
        Bundle args = new Bundle();
        args.putString(ARG_KEY, key);

        PictureFragment fragment = new PictureFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public PictureFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        mKey = args.getString(ARG_KEY);
        mPage = mCallbacks.onGetPage(mKey);

        PicturePage picturePage = (PicturePage) mPage;
        label = picturePage.getLabel();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page_empty, container, false);
        ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

        final LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainLL);
        uib = new UploadImageButton( getActivity(),300,CAM_COMMAND, GAL_COMMAND, INT_COMMAND, this);
        TextView mNameView = ((TextView) rootView.findViewById(R.id.label));
        mNameView.setText(label);

        uib.callback = new com.example.goodog.interfaces.ChosenCallback() {
			
			@Override
			public void NotChosencallbackcall() {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void Chosencallbackcall()
			{
				mPage.getData().putParcelable(Page.IMAGE_DATA_KEY, uib.GetBitmap(100));
				mPage.notifyDataChanged();
			}
		};
		
		uib.getInfoCallback = new GetInfoCallback() {
			
			@Override
			public String GetInfo(String key) {
				PicturePage picturePage = (PicturePage) mPage;
				return picturePage.GetCrossPagesBundle().getString(key,"");
			}
		};
		
		Bitmap bitmap = (Bitmap)mPage.getData().getParcelable(Page.IMAGE_DATA_KEY);
		if(bitmap != null)
		{
			uib.SetBitmap(bitmap);
		}
		
		
        ll.addView(uib);
        
        
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!(activity instanceof PageFragmentCallbacks)) {
            throw new ClassCastException("Activity must implement PageFragmentCallbacks");
        }

        mCallbacks = (PageFragmentCallbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	if (requestCode == CAM_COMMAND)
    	{
			uib.onCameraActivityResult(requestCode, resultCode, data);
		}
    	if (requestCode == GAL_COMMAND)
    	{
			uib.onGalleryActivityResult(requestCode, resultCode, data);
		}
    	if (requestCode == INT_COMMAND)
    	{
			uib.onInternetActivityResult(requestCode, resultCode, data);
		}
    }
    
    
}
